// pages/index/join/index.js
const app = getApp()
const WxParse = require('../../wxParse/wxParse');
import Toast from "../../../vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    mobile: '',
    city: '',
    message: '',
    joinNum: "",
    isJoin: false,
    isShare: false,
    invitation_code: "",
    details: [{
      title: "是否已选好场地?",
      choose: [{
        name: '是',
        isSel: false,
        value: 1
      }, {
        name: '否',
        isSel: false,
        value: 0
      }]
    }, {
      title: "预计开店预算?",
      choose: [{
        name: '10-15万',
        isSel: false,
        value: 1
      }, {
        name: '20-30万',
        isSel: false,
        value: 2
      }, {
        name: '30万以上',
        isSel: false,
        value: 3
      }]
    }, {
      title: "是否有去闲来娱乐考察或体验过?",
      choose: [{
        name: '是',
        isSel: false,
        value: 1
      }, {
        name: '否',
        isSel: false,
        value: 0
      }]
    }, {
      title: "之前是否有联系过其他人了解闲来娱乐?",
      choose: [{
        name: '是',
        isSel: false,
        value: 1
      }, {
        name: '否',
        isSel: false,
        value: 0
      }]
    }, ]


  },
  onLoad(options) {
    console.log("invitation_code", options.invitation_code && options.invitation_code == "undefined" && options.invitation_code == "")
    wx.hideShareMenu()
    const that = this
    if (options.invitation_code != undefined && options.invitation_code != "undefined" && options.invitation_code != "") {
      that.setData({
        isJoin: true,
        invitation_code: options.invitation_code
      })
    }

    app.http.post(app.api.AllianceDetail, {}, (res) => {
      console.log(res.data.content)
      that.setData({
        join: res.data
      })
      WxParse.wxParse('content', 'html', res.data.content, that, 5);
      WxParse.wxParse('brief', 'html', res.data.brief, that, 5);
    })
  },
  chooseTab(e) {
    const that = this
    console.log(e.currentTarget.dataset)
    let index = e.currentTarget.dataset.index
    let i = e.currentTarget.dataset.n
    let data = {
      details: that.data.details
    };
    for (let a = 0; a < data.details[index].choose.length; a++) {
      data.details[index].choose[a].isSel = false
    }
    data.details[index].choose[i].isSel = true
    this.setData(data)
  },
  submit() {
    const that = this
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    let is_field = 4
    let is_budget = 4
    let is_study = 4
    let is_solution = 4
    let item = that.data.details
    for (let i = 0; i < item.length; i++) {
      for (let ii = 0; ii < item[i].choose.length; ii++) {
        if (item[i].choose[ii].isSel) {
          if (i === 0) {
            is_field = item[0].choose[ii].value
          } else if (i === 1) {
            is_budget = item[1].choose[ii].value
          } else if (i === 2) {
            is_study = item[2].choose[ii].value
          } else if (i === 3) {
            is_solution = item[3].choose[ii].value
          }
          break
        }
      }
    }

    if (is_field === 4) {
      Toast.fail("请勾选是否已选好场地")
      return
    }

    if (is_budget === 4) {
      Toast.fail("请选择开店预算")
      return
    }
    if (is_study === 4) {
      Toast.fail("请勾选是否体验过闲来娱乐")
      return
    }
    if (is_solution === 4) {
      Toast.fail("请勾选是否了解过闲来娱乐")
      return
    }
    if (that.data.name == "") {
      Toast.fail("请填写姓名")
      return
    }
    if (!reg.test(that.data.mobile)) {
      Toast.fail("请填写正确的手机号")
      return
    }
    if (that.data.city == "") {
      Toast.fail("请填写加盟城市")
      return
    }

    console.log("is_budget", is_field, is_budget, is_study, is_solution)
    app.http.post(app.api.Alliance, {
      name: that.data.name,
      mobile: that.data.mobile,
      address: that.data.city,
      news: that.data.message,
      invitation_code: that.data.joinNum,
      is_field,
      is_budget,
      is_study,
      is_solution
    }, (res) => {
      Toast({
        type: 'success',
        message: res.message,
        forbidClick: true,
      });
    })
  },
  joinNum(e) {
    this.setData({
      joinNum: e.detail.value,
    });
  },
  name(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  mobile(e) {
    this.setData({
      mobile: e.detail.value,
    });
  },
  mobile2(e) {
    const that = this
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    console.log(e)
    if (reg.test(e.detail.value)) {
      app.http.post(app.api.invitation, {
        mobile: e.detail.value
      }, (res) => {
        that.setData({
          invitation_code: res.data.invitation_code,
          mobile: e.detail.value
        })
      })
    } else {
      that.setData({
        invitation_code: "",
        mobile: ""
      })
    }
    // this.setData({
    //   mobile: e.detail.value,
    // });
  },
  city(e) {
    this.setData({
      city: e.detail.value,
    });
  },
  onChange(event) {
    this.setData({
      message: event.detail,
    });
  },
  isJoin() {
    const that = this
    that.setData({
      isJoin: true
    })
  },
  isShare() {
    const that = this
    that.setData({
      isShare: true
    })
  },
  nshare() {
    const that = this
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if (!reg.test(that.data.mobile)) {
      Toast.fail("请填写正确的手机号")
      return
    }
  },
  onShareAppMessage: function (res) {
    const that = this
    // if()
    if (res.from === "menu") {
      console.log(res.from === "menu")
      return
    }
    console.log(303, that.data.invitation_code)
    // 分享
    if (res.from === "button") {
      // 来自页面内转发按钮
      //console.log("转发：" + JSON.stringify(res.target))
    }
    return {
      title: "邀您加盟",
      path: "/pages/index/join/index?invitation_code=" + that.data.invitation_code,
      imageUrl: "",
      success: function (res) {
        //console.log('/pages/order/detail/detail?goodsId=' + this.data.goodsInfo.id )
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        //console.log("转发失败:" + JSON.stringify(res));
      },
    };
  },
});